import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/home/HomeView.vue'
import LoginView from '@/components/views/LoginView.vue'
import RegisterView from '@/components/views/RegisterView.vue'
import AdminLoginView from '@/components/views/AdminLoginView.vue'

// 房源相关页面
import HouseSearchView from '@/views/house/HouseSearchView.vue'
import PublishHouseView from '@/views/house/PublishHouseView.vue'
import PublishSuccessView from '@/views/house/PublishSuccessView.vue'
import MyHousesView from '@/views/house/MyHousesView.vue'
import HouseDetailView from '@/views/house/HouseDetailView.vue'

// 中介相关页面
import AgentApplyView from '@/views/agent/AgentApplyView.vue'
import AgentStatusView from '@/views/agent/AgentStatusView.vue'
import AgentHousesView from '@/views/agent/AgentHousesView.vue'
import AgentHouseApplicationsView from '@/views/agent/AgentHouseApplicationsView.vue'
import AgentDashboardView from '@/views/agent/AgentDashboardView.vue'
import AvailableHousesView from '@/views/agent/AvailableHousesView.vue'
import MyProxyHousesView from '@/views/agent/MyProxyHousesView.vue'
import AppointmentManagementView from '@/views/agent/AppointmentManagementView.vue'

// 租房信息相关页面
import TenantView from '@/views/rental/tenant/TenantView.vue'
import LandlordView from '@/views/rental/landlord/LandlordView.vue'
import AgentRentalView from '@/views/rental/agent/AgentRentalView.vue'

// 消息中心页面
import ChatView from "@/views/message/ChatView.vue";
import SystemMessageView from "@/views/message/SystemMessageView.vue";
import MessageCenterView from "@/views/message/MessageCenterView.vue";

// 个人资料页面
import AgentHouseDetailView from "@/views/agent/AgentHouseDetailView.vue";
import AgentApplicationsView from "@/views/agent/AgentApplicationsView.vue";
import LandlordAgentApplicationsView from "@/views/landlord/AgentApplicationsView.vue";
import RentalAgentHouseDetailView from "@/views/rental/agent/RentalAgentHouseDetailView.vue";
import ProfileView from "@/views/profile/ProfileView.vue";
import LandlordContractView from "@/views/contract/LandlordContractView.vue";
import TenantContractView from "@/views/contract/TenantContractView.vue";

import MaintenanceView from '@/views/rental/tenant/MaintenanceView.vue'
import CheckoutView from '@/views/rental/tenant/CheckoutView.vue'
import MaintenanceManagementView from '@/views/rental/landlord/MaintenanceManagementView.vue'
import CheckoutManagementView from '@/views/rental/landlord/CheckoutManagementView.vue'
import LandlordAppointments from "@/views/rental/landlord/LandlordAppointments.vue";
import AgentHouseApply from "@/views/rental/agent/AgentHouseApply.vue";
import AgentApplication from "@/views/rental/landlord/AgentApplication.vue";
import AgentAppointments from "@/views/rental/agent/AgentAppointments.vue";
import ImageUploadTest from "@/views/test/ImageUploadTest.vue";
import MapTestView from "@/views/test/MapTestView.vue";
import ImageUploadDemo from "@/views/demo/ImageUploadDemo.vue";
import ChatTest from "@/views/test/ChatTest.vue";
import SettingsView from "@/views/profile/SettingsView.vue";
import AdminDashboardView from "@/views/admin/AdminDashboardView.vue";
import ManagerAgent from "@/views/admin/ManagerAgent.vue";
import ManagerHouse from "@/views/admin/ManagerHouse.vue";
import ManagerUser from "@/views/admin/ManagerUser.vue";
import IdentityVerificationView from "@/views/user/IdentityVerificationView.vue";
import SystemSettings from "@/views/admin/SystemSettings.vue";

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/home',
        name: 'Home',
        component: HomeView,
        meta: { requiresAuth: true }
    },
    {
        path: '/login',
        name: 'Login',
        component: LoginView
    },
    {
        path: '/register',
        name: 'Register',
        component: RegisterView
    },
    // 管理员路由
    {
        path: '/admin-login',
        name: 'AdminLogin',
        component: AdminLoginView
    },
    {
        path: '/admin/dashboard',
        name: 'AdminDashboard',
        component: AdminDashboardView,
        meta: { requiresAuth: true, role: 'admin' }
    },
    {
        path: '/admin/house-verification',
        name: 'ManagerHouse',
        component: ManagerHouse,
        meta: { requiresAuth: true, role: 'admin' }
    },
    {
        path: '/admin/agent-verification',
        name: 'ManagerAgent',
        component: ManagerAgent,
        meta: { requiresAuth: true, role: 'admin' }
    },
    {
        path: '/admin/user-management',
        name: 'ManagerUser',
        component: ManagerUser,
        meta: { requiresAuth: true, role: 'admin' }
    },
    {
        path: '/admin/system-settings',
        name: 'SystemSettings',
        component: SystemSettings,
        meta: { requiresAuth: true, role: 'admin' }
    },
    // 房源相关路由
    {
        path: '/house/search',
        name: 'HouseSearch',
        component: HouseSearchView,
        meta: { requiresAuth: true }
    },
    {
        path: '/house/publish',
        name: 'PublishHouse',
        component: PublishHouseView,
        meta: { requiresAuth: true }
    },
    {
        path: '/house/publish-success',
        name: 'PublishSuccess',
        component: PublishSuccessView,
        meta: { requiresAuth: true }
    },
    {
        path: '/house/my-houses',
        name: 'MyHouses',
        component: MyHousesView,
        meta: { requiresAuth: true }
    },
    {
        path: '/house/detail/:id',
        name: 'HouseDetail',
        component: HouseDetailView,
        props: true,
        meta: { requiresAuth: true }
    },
    {
        path: '/house/edit/:id',
        name: 'EditHouse',
        component: () => import('@/views/house/EditHouseView.vue'),
        props: true,
        meta: { requiresAuth: true }
    },
    // 中介相关路由
    {
        path: '/agent/apply',
        name: 'AgentApply',
        component: AgentApplyView,
        meta: { requiresAuth: true }
    },
    {
        path: '/agent/status',
        name: 'AgentStatus',
        component: AgentStatusView,
        meta: { requiresAuth: true }
    },
    {
        path: '/agent/houses',
        name: 'AgentHouses',
        component: AgentHousesView,
        meta: { requiresAuth: true }
    },
    {
        path: '/agent/house-applications',
        name: 'AgentHouseApplications',
        component: AgentHouseApplicationsView,
        meta: { requiresAuth: true }
    },
    {
        path: '/agent/house-detail/:id',
        name: 'AgentHouseDetail',
        component: AgentHouseDetailView,
        meta: { requiresAuth: true }
    },
    {
        path: '/agent/applications',
        name: 'AgentApplications',
        component: AgentApplicationsView,
        meta: { requiresAuth: true }
    },
    // 新增中介工作台相关路由
    {
        path: '/agent/dashboard',
        name: 'AgentDashboard',
        component: AgentDashboardView,
        meta: { requiresAuth: true, role: 'agent' }
    },
    {
        path: '/agent/available-houses',
        name: 'AvailableHouses',
        component: AvailableHousesView,
        meta: { requiresAuth: true, role: 'agent' }
    },
    {
        path: '/agent/my-proxy-houses',
        name: 'MyProxyHouses',
        component: MyProxyHousesView,
        meta: { requiresAuth: true, role: 'agent' }
    },
    {
        path: '/agent/appointment-management',
        name: 'AppointmentManagement',
        component: AppointmentManagementView,
        meta: { requiresAuth: true, role: 'agent' }
    },
    // LandlordHouseDetailView
    {
        path: '/agent/house-applications',
        name: 'AgentHouseApply',
        component: AgentHouseApply,
        meta: { requiresAuth: true }
    },
    // 租房信息相关路由
    {
        path: '/rental/tenant',
        name: 'Tenant',
        component: TenantView,
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/landlord',
        name: 'Landlord',
        component: LandlordView,
        meta: { requiresAuth: true }
    },

    {
        path: '/rental/agent/house-detail/:id',
        name: 'RentalAgentHouseDetail',
        component: RentalAgentHouseDetailView,
        meta: { requiresAuth: true }
    },
    /*{
        path: '/contract',
        name: 'SignContract',
        component: SignContractView,
        meta: { requiresAuth: true }
    },*/
    // 合同相关路由
    {
        path: '/contract/tenant',
        name: 'TenantContract',
        component: TenantContractView,
        meta: { requiresAuth: true, role: 'tenant' }
    },
    {
        path: '/contract/landlord',
        name: 'LandlordContract',
        component: () => import('@/views/landlord/contract/ContractApplications.vue'),
        meta: { requiresAuth: true, role: 'landlord' }
    },
    // 消息中心路由
    {
        path: '/message',
        name: 'Message',
        component: MessageCenterView,
        meta: { requiresAuth: true }
    },
    {
        path: '/message/system',
        name: 'SystemMessage',
        component: SystemMessageView,
        meta: { requiresAuth: true }
    },
    {
        path: '/message/chat/:id',
        name: 'Chat',
        component: ChatView,
        meta: { requiresAuth: true }
    },
    // 个人资料相关路由
    {
        path: '/profile',
        name: 'Profile',
        component: ProfileView,
        meta: { requiresAuth: true }
    },
    {
        path: '/settings',
        name: 'Settings',
        component: SettingsView,
        meta: { requiresAuth: true }
    },
    // 实名认证路由
    {
        path: '/user/identity-verification',
        name: 'IdentityVerification',
        component: IdentityVerificationView,
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/tenant/maintenance',
        name: 'TenantMaintenance',
        component: MaintenanceView,
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/tenant/checkout',
        name: 'TenantCheckout',
        component: CheckoutView,
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/landlord/maintenance',
        name: 'LandlordMaintenance',
        component: MaintenanceManagementView,
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/landlord/checkout',
        name: 'LandlordCheckout',
        component: CheckoutManagementView,
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/landlord/agent-applications',
        name: 'AgentApplication',
        component: AgentApplication,
        meta: { requiresAuth: true }
    },
    {
        path: '/landlord/agent-applications',
        name: 'LandlordAgentApplications',
        component: LandlordAgentApplicationsView,
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/tenant/maintenance-results',
        name: 'TenantMaintenanceResults',
        component: () => import('@/views/rental/tenant/TenantMaintenanceResults.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/rental/tenant/checkout-results',
        name: 'TenantCheckoutResults',
        component: () => import('@/views/rental/tenant/TenantCheckoutResults.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/tenant/appointment',
        name: 'TenantAppointment',
        component: () => import('@/views/rental/tenant/TenantAppointment.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/landlord/appointments',
        name: 'LandlordAppointments',
        component: LandlordAppointments,
        meta: { requiresAuth: true }
    },
    {
        path: '/agent/appointments',
        name: 'AgentAppointments',
        component: AgentAppointments,
        meta: { requiresAuth: true }
    },
    {
        path: '/test/image-upload',
        name: 'ImageUploadTest',
        component: () => import('@/views/test/ImageUploadTest.vue')
    },
    {
        path: '/test/chat',
        name: 'ChatTest',
        component: () => import('@/views/test/ChatTest.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/test/notification',
        name: 'SystemNotificationTest',
        component: () => import('@/views/test/SystemNotificationTest.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/chat',
        name: 'Chat',
        component: () => import('@/views/message/ChatView.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/message',
        name: 'MessageCenter',
        component: () => import('@/views/message/MessageCenterView.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/demo/image-upload',
        name: 'ImageUploadDemo',
        component: ImageUploadDemo
    },
    {
        path: '/test',
        name: 'TestIndex',
        component: () => import('@/views/test/TestIndexView.vue'),
        meta: { title: '测试中心' }
    },
    {
        path: '/test/api',
        name: 'ApiTest',
        component: () => import('@/views/test/ApiTestView.vue')
    },
    {
        path: '/test/admin-api',
        name: 'AdminApiTest',
        component: () => import('@/views/test/AdminApiTest.vue')
    },
    {
        path: '/test/connection',
        name: 'BackendConnectionTest',
        component: () => import('@/views/test/BackendConnectionTest.vue')
    },
    {
        path: '/test/message',
        name: 'MessageTest',
        component: () => import('@/views/test/MessageTestView.vue'),
        meta: { title: '消息系统测试' }
    },
    {
        path: '/test/register',
        name: 'RegisterTest',
        component: () => import('@/views/test/RegisterTestView.vue'),
        meta: { title: '注册功能测试' }
    },
    {
        path: '/test/password-reset',
        name: 'PasswordReset',
        component: () => import('@/views/test/PasswordResetView.vue'),
        meta: { title: '密码重置工具' }
    },
    {
        path: '/test/register-debug',
        name: 'RegisterDebug',
        component: () => import('@/views/test/RegisterDebugView.vue'),
        meta: { title: '注册API调试' }
    },
    {
        path: '/test/api-debug',
        name: 'ApiDebug',
        component: () => import('@/views/test/ApiDebugView.vue'),
        meta: { title: 'API调试工具' }
    },
    {
        path: '/test/backend-test',
        name: 'BackendTest',
        component: () => import('@/views/test/BackendTestView.vue'),
        meta: { title: '后端连接测试' }
    },
    {
        path: '/test/backend-connection',
        name: 'TestBackendConnection',
        component: () => import('@/views/test/TestBackendConnectionView.vue'),
        meta: { title: '后端连接对比测试' }
    },
    {
        path: '/test/simple-backend',
        name: 'SimpleBackendTest',
        component: () => import('@/views/test/SimpleBackendTestView.vue'),
        meta: { title: '简单后端测试' }
    },
    {
        path: '/test/login-chat',
        name: 'LoginChatTest',
        component: () => import('@/views/test/LoginChatTestView.vue'),
        meta: { title: '登录聊天测试' }
    },
    {
        path: '/test/fix-verification',
        name: 'FixTest',
        component: () => import('@/views/test/FixTestView.vue'),
        meta: { title: '修复验证测试' }
    },
    {
        path: '/test/admin-login',
        name: 'AdminLoginTest',
        component: () => import('@/views/test/AdminLoginTestView.vue'),
        meta: { title: '管理员登录测试' }
    },
    {
        path: '/test/admin-complete',
        name: 'AdminCompleteTest',
        component: () => import('@/views/test/AdminCompleteTestView.vue'),
        meta: { title: '管理员完整功能测试' }
    },
    {
        path: '/test/admin-debug',
        name: 'AdminLoginDebug',
        component: () => import('@/views/test/AdminLoginDebugView.vue'),
        meta: { title: '管理员登录调试' }
    },
    {
        path: '/test/admin-data',
        name: 'AdminDataTest',
        component: () => import('@/views/test/AdminDataTestView.vue'),
        meta: { title: '管理员数据测试' }
    },
    {
        path: '/test/database-schema',
        name: 'DatabaseSchemaTest',
        component: () => import('@/views/test/DatabaseSchemaTestView.vue'),
        meta: { title: '数据库表结构检查' }
    },
    {
        path: '/test/contract',
        name: 'ContractTest',
        component: () => import('@/views/test/ContractTest.vue'),
        meta: { title: '签约功能测试' }
    },
    {
        path: '/test/message-flow',
        name: 'MessageFlowTest',
        component: () => import('@/views/test/MessageFlowTestView.vue'),
        meta: { title: '消息业务完整流程测试', requiresAuth: true }
    },
    {
        path: '/test/chat-message',
        name: 'ChatMessageTest',
        component: () => import('@/views/test/ChatMessageTestView.vue'),
        meta: { title: '聊天消息测试', requiresAuth: false }
    },
    {
        path: '/test/contract-flow',
        name: 'ContractFlowTest',
        component: () => import('@/views/test/ContractFlowTest.vue'),
        meta: { title: '签约申请流程测试' }
    },
    {
        path: '/tenant/contract/applications',
        name: 'TenantContractApplications',
        component: () => import('@/views/tenant/contract/MyApplications.vue'),
        meta: { title: '我的签约申请', requiresAuth: true }
    },
    {
        path: '/test/house-detail-diagnosis',
        name: 'HouseDetailDiagnosis',
        component: () => import('@/views/test/HouseDetailDiagnosis.vue'),
        meta: { title: '房源详情诊断工具' }
    },
    {
        path: '/test/login-kick',
        name: 'LoginKickTest',
        component: () => import('@/views/test/LoginKickTestView.vue'),
        meta: { title: '登录踢掉测试', requiresAuth: false }
    },
    {
        path: '/test/agent-status-debug',
        name: 'AgentStatusDebug',
        component: () => import('@/views/test/AgentStatusDebugView.vue'),
        meta: { title: '中介申请状态调试', requiresAuth: true }
    },
    {
        path: '/test/agent-application',
        name: 'AgentApplicationTest',
        component: () => import('@/views/test/AgentApplicationTestView.vue'),
        meta: { title: '中介代理申请功能测试', requiresAuth: true }
    },

    // 地图测试路由
    {
        path: '/test/map',
        name: 'MapTest',
        component: MapTestView,
        meta: { title: '地图组件测试' }
    },

    // 消息相关路由
    {
        path: '/messages',
        name: 'MessageCenter',
        component: () => import('@/components/views/MessageCenterView.vue'),
        meta: { requiresAuth: true, title: '消息中心' }
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

export default router
